<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="最近经过跟组内大佬亲切交谈后，大佬建议前端应该要了解一下后端。而对于前端人员来说，最快的方式可能就是通过nodejs了。 说干就干，都说兴趣是最好的老师，对于枯燥的文档实在是很难坚持看下去，所以我觉得先通过node爬虫的方式简单的，打个招呼吧">
<meta property="og:type" content="article">
<meta property="og:title" content="nodejs  | 我用node写爬虫">
<meta property="og:url" content="https://inkgn.gitee.io/2021/11/28/article33/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="最近经过跟组内大佬亲切交谈后，大佬建议前端应该要了解一下后端。而对于前端人员来说，最快的方式可能就是通过nodejs了。 说干就干，都说兴趣是最好的老师，对于枯燥的文档实在是很难坚持看下去，所以我觉得先通过node爬虫的方式简单的，打个招呼吧">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-11-28T03:49:11.000Z">
<meta property="article:modified_time" content="2021-11-28T04:46:00.696Z">
<meta property="article:author" content="yinks">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://inkgn.gitee.io/2021/11/28/article33/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>nodejs  | 我用node写爬虫 | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2021/11/28/article33/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          nodejs  | 我用node写爬虫
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-11-28 11:49:11 / 修改时间：12:46:00" itemprop="dateCreated datePublished" datetime="2021-11-28T11:49:11+08:00">2021-11-28</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>最近经过跟组内大佬亲切交谈后，大佬建议前端应该要了解一下后端。而对于前端人员来说，最快的方式可能就是通过nodejs了。</p>
<p>说干就干，都说兴趣是最好的老师，对于枯燥的文档实在是很难坚持看下去，所以我觉得先通过node爬虫的方式简单的，打个招呼吧</p>
<h4 id="起步"><a href="#起步" class="headerlink" title="起步"></a>起步</h4><p>进入目录</p>
<blockquote>
<p>cd D:\code_study\learning_classRoom</p>
</blockquote>
<p>创建一个文件夹用于存放项目目录 </p>
<blockquote>
<p>mkdir demo</p>
</blockquote>
<p>进入项目</p>
<blockquote>
<p>cd demo</p>
</blockquote>
<p>首先用npm初始化项目</p>
<blockquote>
<p>npm init -y</p>
</blockquote>
<p>此时目录中会生成一个package.json文件，里面有关于本项目的一些基本信息！</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>: <span class="string">&quot;node_demo&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;description&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;keywords&quot;</span>: [],</span><br><span class="line">  <span class="string">&quot;author&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">  <span class="string">&quot;license&quot;</span>: <span class="string">&quot;ISC&quot;</span>,</span><br><span class="line">  <span class="string">&quot;dependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="comment">//   默认是没有这两个的，这是我后来额外引入的，下面会讲到</span></span><br><span class="line">    <span class="string">&quot;cheerio&quot;</span>: <span class="string">&quot;^1.0.0-rc.10&quot;</span>,</span><br><span class="line">    <span class="string">&quot;request&quot;</span>: <span class="string">&quot;^2.88.2&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>正常情况，到这里就应该安装我们的webpack了，但是由于我们仅仅是写一个小小的爬虫文件，实在是没必要做打包处理了</p>
<p>这里直接根据提示创建index.js文件</p>
<h4 id="引入模块"><a href="#引入模块" class="headerlink" title="引入模块"></a>引入模块</h4><p><code>这里先大致了解下模块的功能就好，不做过多介绍</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// node 内置自带的模块，可用于发起服务和请求，具体可查看官方文档</span></span><br><span class="line"><span class="keyword">const</span> https = <span class="built_in">require</span>(<span class="string">&#x27;https&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入</span></span><br><span class="line">npm install cheerio</span><br><span class="line"><span class="comment">// 主要用于获取dom节点，类似node中的jQuery</span></span><br><span class="line"><span class="keyword">const</span> fliePath = <span class="built_in">require</span>(<span class="string">&#x27;cheerio&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// node内置模块，用于文件操作，增删改查文件或文件夹</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入</span></span><br><span class="line">npm install request</span><br><span class="line"><span class="comment">// 外部引入的请求模块，用于请求</span></span><br><span class="line"><span class="keyword">const</span> request = <span class="built_in">require</span>(<span class="string">&#x27;request&#x27;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="发起请求"><a href="#发起请求" class="headerlink" title="发起请求"></a>发起请求</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 过于色情，网址打个码 -_-</span></span><br><span class="line">https.get(<span class="string">&#x27;https://www.xxxx.net/&#x27;</span>, <span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> html = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="comment">// 页面会分段返回这里用html字符串拼接起来</span></span><br><span class="line">    res.on(<span class="string">&#x27;data&#x27;</span>, <span class="function"><span class="params">chunk</span> =&gt;</span> &#123;</span><br><span class="line">        html += chunk;</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 请求完成后触发end回调</span></span><br><span class="line">    res.on(<span class="string">&#x27;end&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> imgList = [];</span><br><span class="line">        <span class="comment">// 因为后面又很多操作需要依赖前一步的结果操作，这里使用promis链式调用</span></span><br><span class="line">        <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="comment">// 载入html文档</span></span><br><span class="line">            <span class="keyword">const</span> $ = fliePath.load(html);</span><br><span class="line">            <span class="comment">// 通过标签选择器选择所有图片父容器进行循环遍历</span></span><br><span class="line">            $(<span class="string">&#x27;.swiper-wrapper .swiper-slide&#x27;</span>).each(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="comment">// 拿到图片的src地址，这种大量图片的网站大多数都是用了懒加载技术，所以爬取的时候不要直接拿src</span></span><br><span class="line">                <span class="keyword">const</span> img = $(<span class="string">&#x27;img&#x27;</span>, <span class="built_in">this</span>).attr(<span class="string">&#x27;data-src&#x27;</span>)</span><br><span class="line">                imgList.push(&#123; img &#125;)</span><br><span class="line">            &#125;)</span><br><span class="line">            resolve(<span class="string">&#x27;图片获取成功！&#x27;</span>)</span><br><span class="line">            &#125;).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(res)</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">                    <span class="comment">// fs模块检查是否存在imgs文件夹</span></span><br><span class="line">                    fs.stat(<span class="string">&#x27;./imgs&#x27;</span>, <span class="function">(<span class="params">err, stats</span>) =&gt;</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!err) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (stats.isDirectory()) &#123;</span><br><span class="line">                                resolve(<span class="string">&#x27;imgs 目录已存在！&#x27;</span>)</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            <span class="comment">// 创建新目录文件夹</span></span><br><span class="line">                            fs.mkdir(<span class="string">&#x27;./imgs&#x27;</span>, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">                                <span class="keyword">if</span> (!err) &#123;</span><br><span class="line">                                    resolve(<span class="string">&#x27;新建目录成功！&#x27;</span>)</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;)</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;)</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(res);</span><br><span class="line">            <span class="comment">// 循环图片列表</span></span><br><span class="line">            imgList.forEach(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">let</span> picUrl = item.img</span><br><span class="line">                <span class="comment">// 使用request方式请求图片文件</span></span><br><span class="line">                request.head(picUrl, <span class="function"><span class="keyword">function</span>(<span class="params">err, res, body</span>) </span>&#123;</span><br><span class="line">                    <span class="comment">// 使用文件流的形似保存图片到本地来</span></span><br><span class="line">                    request(picUrl).pipe(fs.createWriteStream(__dirname + <span class="string">&#x27;/imgs&#x27;</span> + <span class="string">&quot;/&quot;</span> + (index * <span class="built_in">Math</span>.random() * <span class="number">10</span>) + <span class="string">&#x27;.jpg&#x27;</span>));</span><br><span class="line">                    <span class="built_in">console</span>.log(<span class="string">`第<span class="subst">$&#123;index&#125;</span>张下载成功！`</span>);</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="实测"><a href="#实测" class="headerlink" title="实测"></a>实测</h4><p>图片过于色情，这里就不放出来了  </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">PS D:\code_study\learning_classRoom\node_demo&gt; node index.js</span><br><span class="line">图片获取成功！</span><br><span class="line">imgs 目录已存在！</span><br><span class="line">第<span class="number">0</span>张下载成功！</span><br><span class="line">第<span class="number">9</span>张下载成功！</span><br><span class="line">第<span class="number">1</span>张下载成功！</span><br><span class="line">第<span class="number">3</span>张下载成功！</span><br><span class="line">第<span class="number">2</span>张下载成功！</span><br><span class="line">第<span class="number">11</span>张下载成功！</span><br><span class="line">第<span class="number">15</span>张下载成功！</span><br><span class="line">第<span class="number">6</span>张下载成功！</span><br><span class="line">第<span class="number">5</span>张下载成功！</span><br><span class="line">第<span class="number">8</span>张下载成功！</span><br><span class="line">第<span class="number">13</span>张下载成功！</span><br><span class="line">第<span class="number">7</span>张下载成功！</span><br><span class="line">第<span class="number">4</span>张下载成功！</span><br><span class="line">第<span class="number">10</span>张下载成功！</span><br><span class="line">第<span class="number">12</span>张下载成功！</span><br><span class="line">第<span class="number">14</span>张下载成功！</span><br><span class="line">PS D:\code_study\learning_classRoom\node_demo&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h4><p>其实在写的过程中也不是一帆风顺，在存储图片那一步就遇到了问题之前存储图片我采用的是https请求+fs模块存储到本地的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 已废弃的写法，实测这种方式是可以成功保存豆瓣电影TOP25的封面图片到本地的</span></span><br><span class="line"><span class="comment">// let sum = picUrl.indexOf(&#x27;/wp-content&#x27;);</span></span><br><span class="line"><span class="comment">// console.log(sum)</span></span><br><span class="line"><span class="comment">// let options = &#123;</span></span><br><span class="line"><span class="comment">//     hostname: &#x27;img.dounimei.guokoo.com&#x27;,</span></span><br><span class="line"><span class="comment">//     path: picUrl.slice(sum),</span></span><br><span class="line"><span class="comment">//     Headers: &#123;</span></span><br><span class="line"><span class="comment">//         &#x27;user-agent&#x27;: &#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36&#x27;</span></span><br><span class="line"><span class="comment">//     &#125;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// https.get(picUrl, res =&gt; &#123;</span></span><br><span class="line"><span class="comment">//     res.setEncoding = &#x27;binary&#x27;;</span></span><br><span class="line"><span class="comment">//     let str = &#x27;&#x27;</span></span><br><span class="line"><span class="comment">//     res.on(&#x27;data&#x27;, chunk =&gt; &#123;</span></span><br><span class="line"><span class="comment">//         str += chunk</span></span><br><span class="line"><span class="comment">//     &#125;)</span></span><br><span class="line"><span class="comment">//     res.on(&#x27;end&#x27;, () =&gt; &#123;</span></span><br><span class="line"><span class="comment">//         fs.writeFile(`./imgs/$&#123;index&#125;.jpg`, str, &#x27;binary&#x27;, err =&gt; &#123;</span></span><br><span class="line"><span class="comment">//             if (!err) &#123;</span></span><br><span class="line"><span class="comment">//                 console.log(`第$&#123;index&#125;张图片下载成功，剩余$&#123;imgList.length - index&#125;张待下载`)</span></span><br><span class="line"><span class="comment">//             &#125;</span></span><br><span class="line"><span class="comment">//         &#125;)</span></span><br><span class="line"><span class="comment">//     &#125;)</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>但是发现图片下载下来后根本没有办法打开，经过一番搜索也没有找到问题原因和解决办法，这才有了前面使用https请求整个页面，到图片这里又改用request的一幕，因为经过测试发现通过文件流的形式保存到本地是能够正常看的。</p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2021/11/28/article33/" title="nodejs  | 我用node写爬虫">https://inkgn.gitee.io/2021/11/28/article33/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/11/27/article32/" rel="prev" title="flexible+postcss-plugin-px2rem+rem 大屏，H5多端适配实现">
      <i class="fa fa-chevron-left"></i> flexible+postcss-plugin-px2rem+rem 大屏，H5多端适配实现
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/11/30/article34/" rel="next" title="Vue  | vue中的文字滚动广播实现">
      Vue  | vue中的文字滚动广播实现 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B5%B7%E6%AD%A5"><span class="nav-number">1.</span> <span class="nav-text">起步</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BC%95%E5%85%A5%E6%A8%A1%E5%9D%97"><span class="nav-number">2.</span> <span class="nav-text">引入模块</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%91%E8%B5%B7%E8%AF%B7%E6%B1%82"><span class="nav-number">3.</span> <span class="nav-text">发起请求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E6%B5%8B"><span class="nav-number">4.</span> <span class="nav-text">实测</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%97%AE%E9%A2%98"><span class="nav-number">5.</span> <span class="nav-text">问题</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
